﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using Aden.Util.Common;
using Aden.Model.Net;
using Aden.Model.Stock;
using Aden.BLL.StockData;

namespace AdenWebAPI.Controllers
{
    public class StockTransController : BaseController
    {
        [HttpPost]
        public Response GetDates(string token, [FromBody] StockParam param)
        {
            Response response = new Response();
            try
            {
                if (string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
                    throw new Exception("Invalid Token");

                var data = StockTransHelper.instatnce(param).GetDates();
                if (data == null)
                    throw new Exception("No Data");

                response.code = "200";
                response.content = data;
            }
            catch (Exception e)
            {
                response.code = "500";
                response.message = e.Message;
            }
            return response;
        }

        [HttpPost]
        public Response GetItems(string token, [FromBody] StockParam param)
        {
            Response response = new Response();
            try
            {
                if (string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
                    throw new Exception("Invalid Token");

                var data = StockTransHelper.instatnce(param).GetItems(param);
                if (data == null)
                    throw new Exception("No Data");

                response.code = "200";
                response.content = data;
            }
            catch(Exception e)
            {
                response.code = "500";
                response.message = e.Message;
            }
            return response;
        }

        [HttpPost]
        public Response save(string token, [FromBody] StockParam param)
        {
            Response response = new Response();
            try
            {
                if (string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
                    throw new Exception("Invalid Token");

                Dictionary<string,object> dic = StockTransHelper.instatnce(param).save(param);
                if(dic != null && dic.ContainsKey("Error"))
                {
                    if (dic.ContainsKey("data"))
                        response.content = dic["data"];

                    throw new Exception(dic["Error"].ToStringTrim());
                }
                response.code = "200";
                response.message = "Succeed to save ";
            }
            catch (Exception e)
            {
                response.code = "500";
                response.message = e.Message;
            }

            return response;
        }

        [HttpPost]
        public Response ShowComponents(string token, [FromBody] StockParam param)
        {
            Response response = new Response();
            try
            {
                if (string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
                    throw new Exception("Invalid Token");

                var data = StockTransHelper.instatnce(param).ShowComponents();
                if (data == null)
                    throw new Exception("No Data");

                response.code = "200";
                response.content = data;
            }
            catch (Exception e)
            {
                response.code = "500";
                response.message = e.Message;
            }

            return response;
        }
    }
}